﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace OEMS
{
   public class MetingDB
       
    {
        public static void AddMotorMeting(Meting meting)
        {
            /// <summary>
            /// Motor meting toevoegen aan database
            /// </summary>
            /// <param name="metingenMotor"></param>
            /// <author>Robin Goris</author>

            SqlConnection connection = OemsDB.GetConnection();
            SqlTransaction insertTransaction = null;
            string insertStatement = " INSERT Metingen " +
                "(Toerental, Moment, Rendement, GegevensID ) " +
                "VALUES ( @Toerental, @Moment, @Rendement, @GegevensID ) ";
            SqlCommand insertCommand = new SqlCommand(insertStatement, connection);

            insertCommand.Parameters.AddWithValue("@Toerental", meting.Toerental);
            insertCommand.Parameters.AddWithValue("@Moment", meting.Moment);
            insertCommand.Parameters.AddWithValue("@Rendement", meting.Rendement);
            insertCommand.Parameters.AddWithValue("@GegevensID", meting.GegevensId);

            try
            {
                connection.Open();
                insertTransaction = connection.BeginTransaction();
                insertCommand.Connection = connection;
                insertCommand.Transaction = insertTransaction;

                insertCommand.ExecuteNonQuery();
                insertTransaction.Commit();
            }

            catch (SqlException ex)
            {
                if (insertTransaction != null)
                    insertTransaction.Rollback();
                throw ex;
            }
            finally
            {
                connection.Close();
            }

        }

        public static List<Meting> GetMetingen(int gegevensId)
        {
            /// <summary>
            /// Metingen opvragen aan de hand van een gegevensId
            /// </summary>
            /// <param name="gegevensId"></param>
            /// <author>Ronald Geladé</author>

            Meting[] metingen = new Meting[1746];
            List<Meting> metingenList = new List<Meting>();
            SqlConnection conn = OemsDB.GetConnection();
            string selectStatement = "SELECT * " +
                                       " FROM Metingen " +
                                        "WHERE GegevensID = @GegevensID ";

            SqlCommand selectCommand = new SqlCommand(selectStatement, conn);
            selectCommand.Parameters.AddWithValue("@GegevensID", gegevensId);
            try
            {

                conn.Open();
                SqlDataReader reader = selectCommand.ExecuteReader();
                while (reader.Read())
                {

                    Meting meting = new Meting();

                    meting.MetingsId = Convert.ToInt32(reader["MetingsID"]);
                    meting.Moment = (decimal)reader["Moment"];
                    meting.Toerental = (decimal)reader["Toerental"];
                    meting.Rendement = (decimal)reader["Rendement"];
                    meting.GegevensId = Convert.ToInt32(reader["GegevensID"]);

                    metingenList.Add(meting);
                }
                reader.Close();
            }
            catch (SqlException Exception)
            {
                throw Exception;
            }
            finally
            {

                conn.Close();
            }

            return metingenList;
        }

        public static Meting[] GetMetingenArray(int gegevensId)
        {
            Meting[] metingen = new Meting[1764];
            List<Meting> metingList = GetMetingen(gegevensId);
            int index = 0;

            foreach(Meting meting in metingList)
            {
                metingen[index] = meting;
                index++;
            }
            return metingen;
        }
    }
}
